package com.cn.longdistancebusstation;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.model.UserInfo;
import com.cn.longdistancebusstation.registerAndLogin.LoginActivity;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/17.
 */

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener{
    private Button btn_register;
    private EditText mPasswordEditText=null;
    private EditText mSurePasswordEditText=null;
    private String mUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingpws);
        getTitleTextView().setText("设置密码");
        mUsername=getIntent().getStringExtra("entry.telephone");

        initview();
    }

    private void initview() {
        mPasswordEditText = (EditText) findViewById(R.id.register_set_pwd);
        mSurePasswordEditText = (EditText) findViewById(R.id.register_sure_pwd);
        btn_register = (Button) findViewById(R.id.register_btn_register);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.register_btn_register:
                register();
                break;
        }


    }
    private void register(){
        final String pwd=mPasswordEditText.getText().toString().trim();
        final String sure_pwd=mSurePasswordEditText.getText().toString().trim();
        if(pwd.equals(" ")||sure_pwd.equals(" ")){
            Toast.makeText(this,"输入内容不能为空",Toast.LENGTH_SHORT).show();
        }if(!pwd.equals(sure_pwd)){
            Toast.makeText(this,"输入的密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
        }else{
            if (pwd.length() >= 6 && pwd.length() <= 18){
              //注册
                getResponseData();
            }else{
                Toast.makeText(this,"设置的密码长度必须在6到18位之间",Toast.LENGTH_LONG).show();

            }
        }


    }

    private void getResponseData() {
        String pwd=mPasswordEditText.getText().toString().trim();
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<Result<UserInfo>> call=httpService.register(mUsername,pwd);
        call.enqueue(new Callback<Result<UserInfo>>() {
            @Override
            public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
                hideHud();
                Gson gson=new Gson();
                Result result=response.body();
                Boolean isSuccess=result.getHead().getSuccess();
                UserInfo info=(UserInfo)result.getBody();
                if(isSuccess){
                    Toast.makeText(SetPasswordActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(SetPasswordActivity.this,LoginActivity.class));
                    finish();
                }else{
                    Toast.makeText(SetPasswordActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
                hideHud();
                Toast.makeText(SetPasswordActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });


    }

    public void showMsg(String msg){
        AlertDialog.Builder bd = new AlertDialog.Builder(SetPasswordActivity.this);
        bd.setTitle("提示");
        bd.setMessage(msg);
        bd.setNegativeButton("确定", null);
        bd.show();
    }

}
